/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.aclclient.shared;

import nl.biz_e.appengine.lib.shared.login.GoogleLoginInfo;
import nl.biz_e.appengine.lib.shared.login.IAuthenticator;
import nl.biz_e.appengine.lib.shared.login.IAuthorisationContext;
import nl.biz_e.appengine.lib.shared.login.IUser;

/**
 * Authenticates a user or group against a context, using the ACL system
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class AclAuthenticator implements IAuthenticator {
    private IAuthorisationContext appContext;

    /**
     * Constructs a new AclAuthenticator instance
     */
    public AclAuthenticator() {
        super();
    }

    /**
     * Constructs a new AclAuthenticator instance
     * 
     * @param appContext
     */
    public AclAuthenticator(IAuthorisationContext appContext) {
        this();
        this.setAppContext(appContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isAuthorized(IUser user, IAuthorisationContext context) {
        if (context == null) {
            if (appContext != null)
                return (isAuthorized(user, appContext));
        }

        // TODO Make something real from this. It's now simply returning true if
        // the user is an admin

        if (user.isGoogleLogin()) {
            if (((GoogleLoginInfo) user).isAdmin())
                return true;
        }

        return false;
    }

    /**
     * @return the appContext
     */
    public IAuthorisationContext getAppContext() {
        return appContext;
    }

    /**
     * @param appContext
     *            the appContext to set
     */
    public void setAppContext(IAuthorisationContext appContext) {
        this.appContext = appContext;
    }

}
